const baseURL = 'https://obuge.com/juzao'

let interceptors = {
  request(req){
    return new Promise(next => {
      // /^http/.test(req.url) ? req.url : (baseURL + req.url)
      req.url = /^http/.test(req.url) ? req.url : (baseURL + req.url)
      next(req)
    })
  },

  response(res){
    return new Promise(next => {
      if(res.statusCode == 404){
        next({errorMsg:'请求接口不存在'})
      }else if(res.statusCode == 500){
        next({errorMsg:res.data || '服务器错误，请联系网站管理员'})
      }else if(res.statusCode == 200){
        next(res.data)
      }else{
        next({errorMsg:res.errMsg || '未知错误'})
      }
    })
  }

}



function get(url) {
  return new Promise((resolve, reject) => {
    wx.request({
      url:/^http/.test(req.url) ? url : (baseURL + req.url),
      data: query,
      success: (res) => {
        interceptors.response(res).then(data => {
          resolve(data)
        })
      },
      fail: (res) => {
        interceptors.response(res).then(data => {
          resolve(data)
        })
      }
    })
  })
}
module.exports = {
  baseURL,
  get
}
